/*
 * @Author: ReinerLau lk850593913@gmail.com
 * @Date: 2022-09-22 12:50:15
 * @LastEditors: ReinerLau lk850593913@gmail.com
 * @LastEditTime: 2023-02-27 11:16:31
 * @FilePath: \robot\src\views\patroling\composables\usePlanAction.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import usePatrolingStore from "@/store/patroling";
import { computed } from "vue";
import { ElNotification, ElMessage } from "element-plus";
import { sendMavlinkMission } from "@/api/mission-mavlink.js";

const patrolingStore = usePatrolingStore();

const caritem = computed(() => {
  return patrolingStore.patrolingInfo.robotid;
});

export default () => {
  const planActionList = [
    {
      title: "创建路径",
      color: "#FF931E",
      value: "createPath",
    },
    {
      title: "下发任务",
      color: "#1F3947",
      value: "createPlan",
    },
    {
      title: "取消路径",
      color: "#878787",
      value: "cancelPlan",
    },
  ];

  /**
   * @description: 创建路径
   * @return {*}
   */
  function handleCreatePath() {
    patrolingStore.clearPath();
    patrolingStore.startCreatePath();
  }

  /**
   * @description: 取消路径
   * @return {*}
   */
  function handleCancelPlan() {
    patrolingStore.clearPath();
  }

  /**
   * @description: 下发任务
   * @return {*}
   */
  async function handleCreatePlan(missionTemplateId) {
    if (patrolingStore.createdPath.length === 0) {
      ElNotification({
        type: "warning",
        title: "提醒",
        message: "路径点为空",
      });
      return;
    }
    if (!caritem.value) {
      ElNotification({
        type: "warning",
        title: "提醒",
        message: "请选择车辆",
      });
      return;
    }
    const data = patrolingStore.createdPath;
    const params = missionTemplateId
      ? {
          missionTemplateId,
        }
      : {};

    await sendMavlinkMission(data, caritem.value, params);
    ElMessage.success({
      message: "发送成功",
    });
    patrolingStore.clearPath();
  }

  return {
    planActionList,
    handleCreatePath,
    handleCancelPlan,
    handleCreatePlan,
  };
};
